package dao.impl;

import dao.BaseDao;
import dao.UserDao;
import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl extends BaseDao implements UserDao {

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    @Override
    public int add(User user) {
        int result=0;
        try {

            conn = super.getConnection();

            String sql="insert into `user` (login_name,password,username,email,phone,sex,status) values(?,?,?,?,?,?,?) ";

            pstmt = conn.prepareStatement(sql);

            pstmt.setObject(1, user.getLoginName());
            pstmt.setObject(2, user.getPassword());
            pstmt.setObject(3, user.getUsername());
            pstmt.setObject(4, user.getEmail());
            pstmt.setObject(5, user.getPhone());
            pstmt.setObject(6, user.getSex());
            pstmt.setObject(7, user.getStatus());

            result = pstmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            super.closeAll(conn, pstmt, rs);
        }
        return result;
    }

    @Override
    public User findByAccount(String Account) {
        User user = null;

        try {
            conn = super.getConnection();
            String sql = "select * from `user` where login_name=? or email=? or phone=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, Account);
            pstmt.setObject(2, Account);
            pstmt.setObject(3, Account);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setEmail(rs.getString("email"));
                user.setLoginName(rs.getString("login_name"));
                user.setPassword(rs.getString("password"));
                user.setPhone(rs.getString("phone"));
                user.setSex(rs.getInt("sex"));
                user.setStatus(rs.getInt("status"));
                user.setUsername(rs.getString("username"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll(conn, pstmt, rs);
        }

        return user;
    }

    @Override
    public int update(User user) {
        int result=0;
        try {

            conn = super.getConnection();

            String sql="update `user` set login_name=?,password=?,username=?,email=?,phone=?,sex=?,status=? where id=? ";

            pstmt = conn.prepareStatement(sql);

            pstmt.setObject(1, user.getLoginName());
            pstmt.setObject(2, user.getPassword());
            pstmt.setObject(3, user.getUsername());
            pstmt.setObject(4, user.getEmail());
            pstmt.setObject(5, user.getPhone());
            pstmt.setObject(6, user.getSex());
            pstmt.setObject(7, user.getStatus());

            pstmt.setObject(8, user.getId());

            result = pstmt.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            super.closeAll(conn, pstmt, rs);
        }
        return result;
    }
}
